package com.eleventwelve.iris.web.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;

import com.eleventwelve.iris.entity.IrisUser;
import com.eleventwelve.iris.service.IrisUserService;

public class AdjustUserImageController extends AdjustImageController {

	private IrisUserService irisUserService;

	@SuppressWarnings("deprecation")
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
			HttpServletResponse response, Object command, BindException errors)
			throws Exception {

		IrisUser user = (IrisUser) command;

		int x = Integer.parseInt(request.getParameter("x"));
		int y = Integer.parseInt(request.getParameter("y"));
		int x2 = Integer.parseInt(request.getParameter("x2"));
		int y2 = Integer.parseInt(request.getParameter("y2"));
		
		processAdjust(user, x, y, x2, y2);
		
		irisUserService.save(user);

		return new ModelAndView(getSuccessView(), "userId", user.getId());
	}
	
	public void setIrisUserService(
			IrisUserService IrisUserService) {
		this.irisUserService = IrisUserService;
	}
	
	@Override
	protected Object formBackingObject(HttpServletRequest request)
			throws Exception {
		return irisUserService.getCurrentUser();
	}
	
	@Override
	protected Map<String, Object> referenceData(HttpServletRequest request,
			Object command, Errors errors) throws Exception {
		Map<String, Object> mapData = new HashMap<String, Object>();

		mapData.put("user", irisUserService.getCurrentUser());
		return mapData;
	}

}